//
//  Spiel4ViewController.m
//  Lextra Junior ueb IT
//
//  Created by Dirk Lieder on 30.05.11.
//  Copyright 2011 Phosphon. All rights reserved.
//

#import "Spiel4ViewController.h"
#import "NavigationViewController.h"

#import "Lextra_Engine.h"

#import "SpielCounter.h"
#import "Karte.h"


#import <stdlib.h>
#import <time.h>

@implementation Spiel4ViewController


@synthesize richtigLabel;
@synthesize view1,view2;
@synthesize spielCounter;
//___NGUYEN_HUY_SON__
@synthesize navigationBar;
@synthesize labelSupper;
@synthesize imageResult;
@synthesize buttonNew;
//___END_NGUYEN_HUY_SON__
//___NGUYEN_HUY_SON___IPAD_ONLY
@synthesize woerView;
@synthesize Button1;
@synthesize Button2;
@synthesize Button3;
@synthesize Button4;
@synthesize Button5;
@synthesize Button6;
@synthesize Button7;
@synthesize Button8;
@synthesize Button9;
@synthesize Button10;
@synthesize Button11;
@synthesize Button12;
//END_NGUYEN_HUY_SON
AVAudioPlayer *audioPlayer;


// quest pool
static NSMutableArray *myArray;
static int myArrayPtr;

// akt cards
static NSMutableArray *questArray,*spriteArray,*cardArray;

// time and animation
static bool animationRunning;
static int animationCounter,allTime;

// gameplay
static int cardActive1,cardActive2;
static int cardLeft;

// callback info
static int viewCount;
static NSMutableArray *viewObj;

// sound playing
static bool soundPlaying;


// pos-definitions
static int posArray[12][2] = {{18,6},{115,6},{212,6},{18,101},{115,101},{212,101},{18,196},{115,196},{212,196},{18,291},{115,291},{212,291}};
//___NGUYEN_HUY_SON___IPAD_ONLY

//static int posArray_IPAD_Lanscape[12][2] = {{122,50},{277,50},{432,50},{122,205},{277,205},{432,205},{122,360},{277,360},{432,360},{122,515},{277,515},{432,515}};

static int posArray_IPAD_Lanscape[12][2] = {{122,64},{277,64},{432,64},{122,219},{277,219},{432,219},{122,374},{277,374},{432,374},{122,529},{277,529},{432,529}};

//static int posArray_IPAD_Portrait[12][2] = {{154,64},{314,64},{474,64},{154,224},{314,224},{474,224},{154,384},{314,384},{474,384},{154,544},{314,544},{474,544}};

static int posArray_IPAD_Portrait[12][2] = {{154,182},{314,182},{474,182},{154,342},{314,342},{474,342},{154,502},{314,502},{474,502},{154,662},{314,662},{474,662}};

//___END_NGUYEN_HUY_SON
- (void)setDelegatePlud:(id)new_delegate{
	Delegate = new_delegate;
    soundPlaying = false;
    
    // reset Game
    if(myArray) [myArray dealloc];
    //__NGUYEN_HUY_SON___
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) //Neu iPad
    {
        myArray = [[NSMutableArray alloc] initWithArray:[woerView getGameListPoinds] copyItems:YES]; 
    }
    else
    {
        myArray = [[NSMutableArray alloc] initWithArray:[(Lextra_Engine*)Delegate getGameListPoinds] copyItems:YES]; 
    }    
    //___END_NGUYEN_HUY_SON
    NSMutableArray *tmpArray = [[NSMutableArray alloc] init];

    int i=[myArray count];
    while (i>0) {
        i--;
        int tmpObj = arc4random() % ([myArray count]);
        
        [tmpArray addObject:[myArray objectAtIndex:tmpObj]];
        [myArray removeObjectAtIndex:tmpObj];        
    }
    myArrayPtr = 0;
    myArray = tmpArray;
    allTime = 0;
    //__NGUYEN__HUY_SON
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    {
        CGFloat navBarHeight = 44.0f;    
        CGRect frame = CGRectMake(navigationBar.frame.origin.x, navigationBar.frame.origin.y, navigationBar.frame.size.width, navBarHeight);
        [navigationBar setFrame:frame];
    }  //___END_NGUYEN_HUY_SON


    [self newQuest];
    
   // NSLog(@"++%@", myArray);
}
//_____NGUYEN_HUY_SON_____IPAD_LANDSCAPE_____
- (BOOL)isPadPortrait
{
    
    return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
            && (self.interfaceOrientation == UIInterfaceOrientationPortrait
                || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown));
}


- (BOOL)isPadLandscape
{
    
    return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
            && (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight
                || self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft));
    
}
//__________________END_NGUYEN_HUY_SON_______
- (void)newQuest{
    
    self.view = view1;
    [self stop:NULL];
        
    // get 6 new questions (6 cards with audio and 6 with video)
    questArray = [[NSMutableArray alloc] init];
    cardArray = [[NSMutableArray alloc] init];

    int i=6;
    while (i>0) {
        i--;
        
        
        int aktCard;
        if([questArray count]>0){
            aktCard = arc4random() % ([questArray count]);
        } else {
            aktCard = 0;
        }
     
        [questArray insertObject:[myArray objectAtIndex:myArrayPtr] atIndex:aktCard];
        [cardArray insertObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"0", @"sound",[NSString stringWithFormat:@"%i", i] , @"position", @"1", @"active", nil] atIndex:aktCard];

        aktCard = arc4random() % ([questArray count]);
        
        [questArray insertObject:[myArray objectAtIndex:myArrayPtr] atIndex:aktCard];
        [cardArray insertObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"1", @"sound",[NSString stringWithFormat:@"%i", i] , @"position", @"1", @"active", nil] atIndex:aktCard];
       
        
        myArrayPtr++;
        if(myArrayPtr >=[myArray count]) myArrayPtr = 0;
    }    
    
    // generate sprites
    spriteArray = [[NSMutableArray alloc] init];
    
    for(i=0;i<[questArray count];i++){
        //__NGUYEN_HUY_SON____
        CGPoint point;
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) //Neu iPad
        {
            if ([self isPadLandscape] == TRUE) 
            {
                point = CGPointMake(posArray_IPAD_Lanscape[i][0], posArray_IPAD_Lanscape[i][1]);
            }
            else
            {
                point = CGPointMake(posArray_IPAD_Portrait[i][0], posArray_IPAD_Portrait[i][1]);
            }
        }
        else
        {
            point = CGPointMake(posArray[i][0], posArray[i][1]);
        }
        //___END_NGUYEN_HUY_SON__
        BOOL media = [[[cardArray objectAtIndex:i] objectForKey:@"sound"] intValue] == 1;
        
        Karte *karte = [[Karte alloc] initWithElement: [questArray objectAtIndex:i]
                                                media: media
                                                speed: CGPointMake(0, 0) 
                                                  pos: point
                                             Delegate: self
                                         DelegateView: self.view];
        
        [spriteArray addObject:karte];
    }    
    cardActive1 = cardActive2 = -1;
    cardLeft = 6;
    viewCount = 0;
    viewObj = [[NSMutableArray alloc] init];
 
    
    // start anim
    allTime = 0;
    animationRunning = false;
    
    NSTimeInterval fps = 0.02;

    timer = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget: self selector: @selector( loop )];
    [timer setFrameInterval: fps];
    [timer addToRunLoop: [NSRunLoop currentRunLoop] forMode: NSDefaultRunLoopMode];

    
    //NSLog(@"++%@", questArray);
}


-(void) loop {
    
    if(!animationRunning){
        animationRunning = true;
        
        animationCounter = 0;
        
        [self.spielCounter setSeconds:0];
        
        if(self.spielCounter == nil){
            
            //___NGUYEN_HUY_SON
            if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
            {
                
                spielCounter = [[SpielCounter alloc] initWithNibName:@"SpielCounter" bundle:nil];
                if([self isPadPortrait] == TRUE)
                {
                    spielCounter.view.frame = CGRectMake(250, 0, navigationBar.frame.size.width-300, 44);
                }
                else
                {
                    spielCounter.view.frame = CGRectMake(230, 0, navigationBar.frame.size.width-300, 44);
                }
                spielCounter.myCounter.frame = spielCounter.view.frame;
                spielCounter.myCounter.textAlignment = UITextAlignmentCenter;
                [spielCounter setDelegate:self];
            }
            else
            {
                SpielCounter *home = [[SpielCounter alloc] initWithNibName:@"SpielCounter" bundle:nil];
                self.spielCounter = home;
                
                CGRect frame = home.view.frame;
                frame.origin.x += 200.0;
                //___END_NGUYEN_HUY_SON
                home.view.frame = frame;
                
                [home setDelegate:self];
                [home release];
            }
            
        }
        //__NGUYEN_HUY_SON___
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
        {
            [navigationBar addSubview:self.spielCounter.view];
        }
        else
        {
            NavigationViewController *tmp = [Delegate getNavigationID];
            [tmp.view addSubview:self.spielCounter.view];
        }
        //END_NGUYEN_HUY_SON__
    }
    
    animationCounter++;
    if(animationCounter > 60*60*50) animationCounter = 60*60*50;
    if(animationCounter > 99) [self.spielCounter setSeconds:animationCounter/50];
	
    for(int i=0;i<[spriteArray count];i++){
        [[spriteArray objectAtIndex:i] renderCard];
    }

}

- (void)touchHandling:(int)card{
    
    int playSound = -1;
         
    if([[[cardArray objectAtIndex:card] objectForKey:@"active"] intValue] == 1){
        if(cardActive1 != -1 && cardActive2 !=-1 && cardActive1 != card && cardActive2 != card) {
            
            [[spriteArray objectAtIndex:cardActive1]hideCard];
            [[spriteArray objectAtIndex:cardActive2]hideCard];
            
            cardActive1 = -1;
            cardActive2 = -1;
        }
        
        if(cardActive1 == -1 || cardActive1 == card){
            cardActive1 = card;
            
            [self stop:NULL]; // stop -> new selection
            
            [[spriteArray objectAtIndex:cardActive1] showCard];
            if([[[cardArray objectAtIndex:cardActive1] objectForKey:@"sound"] intValue] == 1) playSound = cardActive1;
            
        } else if(cardActive2 == -1 || cardActive2 == card){
            cardActive2 = card;
            
            if([[[cardArray objectAtIndex:cardActive1] objectForKey:@"position"]intValue]  == [[[cardArray objectAtIndex:cardActive2] objectForKey:@"position"]intValue]){
                
                viewCount +=2;
                
                [[cardArray objectAtIndex:cardActive1] setObject:@"0" forKey: @"active"];
                [[cardArray objectAtIndex:cardActive2] setObject:@"0" forKey: @"active"];
                
                [viewObj addObject:[NSString stringWithFormat:@"%i", cardActive1]];
                [viewObj addObject:[NSString stringWithFormat:@"%i", cardActive2]];
                
                [[spriteArray objectAtIndex:cardActive1] showCardCallback];
                [[spriteArray objectAtIndex:cardActive2] showCardCallback];
                
               
                if([[[cardArray objectAtIndex:cardActive2] objectForKey:@"sound"] intValue] == 1) playSound = cardActive2;

                cardLeft--;
                
                cardActive1 = cardActive2 = -1;
                
            } else {
                [[spriteArray objectAtIndex:cardActive2] showCard];
                if([[[cardArray objectAtIndex:cardActive2] objectForKey:@"sound"] intValue] == 1) playSound = cardActive2;
            }
            
            
        } else if(cardActive1==card || cardActive2==card) { // repeat sound
            if(cardActive1==card && ([[[cardArray objectAtIndex:cardActive1] objectForKey:@"sound"] intValue] == 1)) playSound = cardActive1;
            if(cardActive2==card && ([[[cardArray objectAtIndex:cardActive2] objectForKey:@"sound"] intValue] == 1)) playSound = cardActive2;
            
        }
    }
    

    if(playSound>=0){
        [self stop:NULL];
        
        NSString *myFilename;
        NSString *myName = [[questArray objectAtIndex:playSound] objectForKey:@"ton"];
        
        if([myName hasSuffix:@".mp3"]){
            myFilename = [myName substringToIndex:[myName length]-4];
        } else {
            myFilename = [[NSString alloc] initWithString:myName];
        }
                
        if(myName){            
            NSURL *url = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:myFilename ofType:@"m4a"]];
            if(audioPlayer) {
                [audioPlayer stop];
                [audioPlayer release];
            }
            audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
            audioPlayer.delegate = self;
            [audioPlayer play];
            soundPlaying = true;
            [url release];
        }
    }
    
    
}

- (void)callback{
    // NSLog(@"++%i--%@", viewCount,viewObj);
    viewCount--;
    if(viewCount == 0 && [viewObj count]>0 ){

        for(int i=0;i<[viewObj count];i++){
            [[spriteArray objectAtIndex:[[viewObj objectAtIndex:i] intValue]] delite];
        }  
        viewObj = [[NSMutableArray alloc] init];
        
        if(cardLeft == 0 && !soundPlaying) 
            [self showResult];
    }    
}


- (IBAction) game1:(id)sender{
    NSLog(@"Card 1");
    [self touchHandling:0];
}
- (IBAction) game2:(id)sender{
    NSLog(@"Card 2");
    [self touchHandling:1];
}
- (IBAction) game3:(id)sender{
    NSLog(@"Card 3");
    [self touchHandling:2];
}
- (IBAction) game4:(id)sender{
    NSLog(@"Card 4");
    [self touchHandling:3];
}
- (IBAction) game5:(id)sender{
    NSLog(@"Card 5");
    [self touchHandling:4];
}
- (IBAction) game6:(id)sender{
    NSLog(@"Card 6");
    [self touchHandling:5];
}
- (IBAction) game7:(id)sender{
    NSLog(@"Card 7");
    [self touchHandling:6];
}
- (IBAction) game8:(id)sender{
    NSLog(@"Card 8");
    [self touchHandling:7];
}
- (IBAction) game9:(id)sender{
    NSLog(@"Card 9");
    [self touchHandling:8];
}
- (IBAction) game10:(id)sender{
    NSLog(@"Card 10");
    [self touchHandling:9];
}
- (IBAction) game11:(id)sender{
    NSLog(@"Card 11");
    [self touchHandling:10];
}
- (IBAction) game12:(id)sender{
    NSLog(@"Card 12");
    [self touchHandling:11];
}

- (IBAction) naechsteswort:(id)sender{
    [self newQuest];
}


- (void)showResult{
    [self sstop];
    
    self.view = view2;
   
    [self stop:NULL]; // stop sound
    
    // auswertung
    allTime = animationCounter/50;

    if([self isPadPortrait] == TRUE)
    {
        imageResult.frame = CGRectMake((768-320)/2, 173, 320, 387);
    }
    else
    {
        imageResult.frame = CGRectMake((703-320)/2, 250, 387, 387);
    }
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"gewonnen" ofType:@"m4a"]]; 

    if(audioPlayer) {
        [audioPlayer stop];
        soundPlaying = false;
        [audioPlayer release];
    }
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    audioPlayer.delegate = self;
    [audioPlayer play];

    NSString *outText;
    int left = floor(animationCounter/60/50);
    int right = (animationCounter/50)-left*60;
    
    outText = [NSString stringWithFormat:@"Du hast %i:%02d Minuten gebraucht.",left,right];
    richtigLabel.text = outText;    
}

- (void)stopAndHideCounter{
    
    if(timer) {
        [timer removeFromRunLoop:[NSRunLoop currentRunLoop] forMode: NSDefaultRunLoopMode];
        timer = Nil;
    }
    if(self.spielCounter) {
        [self.spielCounter.view removeFromSuperview];
    }
    
}


- (IBAction) stop:(id)sender{
    
    if(audioPlayer) [audioPlayer stop];
    soundPlaying = false;
}

- (void)audioPlayerDidFinishPlaying:IntroPlayer successfully: (BOOL)flag
{
    NSLog(@"++AUDIO END");
    
    if(cardLeft == 0 && soundPlaying) 
        [self showResult];
    soundPlaying = false;
}

- (void)sstop{
    
    if(audioPlayer) [audioPlayer stop];
    soundPlaying = false;
    
    [self stopAndHideCounter];
    
    for(int i=0;i<[spriteArray count];i++){
       [[spriteArray objectAtIndex:i] remove];
    }    
    spriteArray = [[NSMutableArray alloc] init];

    
    self.view = view1;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        srandom(time(NULL));
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    {
        [navigationBar setTintColor:[[UIColor alloc] initWithRed:20.0 / 255 green:59.0 / 255 blue:102.0 / 255 alpha:1.0]];
    }

    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

//__NGUYEN_HUY_SON
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    {
        return YES;
    }
    else
    {
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
}
//___END_NGUYEN_HUY_SON___
//___END_NGUYEN_HUY_SON_________________________________________________________________________

//______NGUYEN_HUY_SON___________________________________________________________________________________________
#pragma mark -
#pragma mark Managing the popover

#pragma mark -
#pragma mark Rotate to Portrait
- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
    // Rotate to Portrait
    // Add the popover button to the left navigation item.
    [navigationBar.topItem setLeftBarButtonItem:barButtonItem animated:NO];
        spielCounter.view.frame = CGRectMake(220,0,468,44);
    spielCounter.myCounter.frame = spielCounter.view.frame;
    spielCounter.myCounter.textAlignment = UITextAlignmentCenter;
    
    CGRect newFrame1 = CGRectMake(posArray_IPAD_Portrait[0][0] ,posArray_IPAD_Portrait[0][1] , 150, 150);
    Button1.frame = newFrame1;
    
    imageResult.frame = CGRectMake((768-320)/2, 173, 320, 387);
    labelSupper.frame = CGRectMake((768-labelSupper.frame.size.width)/2, 293, labelSupper.frame.size.width, labelSupper.frame.size.height);
    
    richtigLabel.frame = CGRectMake((768-richtigLabel.frame.size.width)/2, 331, richtigLabel.frame.size.width, richtigLabel.frame.size.height);
    
    buttonNew.frame = CGRectMake((768-buttonNew.frame.size.width)/2, 383, buttonNew.frame.size.width, buttonNew.frame.size.height);
    
    NSLog(@"PORTRAIT:  %@",self.view);

    //__
    newFrame1 = CGRectMake(posArray_IPAD_Portrait[1][0] ,posArray_IPAD_Portrait[1][1] , 150, 150);
    Button2.frame = newFrame1;
    //__
    newFrame1 = CGRectMake(posArray_IPAD_Portrait[2][0] ,posArray_IPAD_Portrait[2][1] , 150, 150);
    Button3.frame = newFrame1;
    //__
    newFrame1 = CGRectMake(posArray_IPAD_Portrait[3][0] ,posArray_IPAD_Portrait[3][1] , 150, 150);
    Button4.frame = newFrame1;
    //__
    newFrame1 = CGRectMake(posArray_IPAD_Portrait[4][0] ,posArray_IPAD_Portrait[4][1] , 150, 150);
    Button5.frame = newFrame1;
    //__
    newFrame1 = CGRectMake(posArray_IPAD_Portrait[5][0] ,posArray_IPAD_Portrait[5][1] , 150, 150);
    Button6.frame = newFrame1;
    //__
    newFrame1 = CGRectMake(posArray_IPAD_Portrait[6][0] ,posArray_IPAD_Portrait[6][1] , 150, 150);
    Button7.frame = newFrame1;
    //__
    newFrame1 = CGRectMake(posArray_IPAD_Portrait[7][0] ,posArray_IPAD_Portrait[7][1] , 150, 150);
    Button8.frame = newFrame1;
    //__
    newFrame1 = CGRectMake(posArray_IPAD_Portrait[8][0] ,posArray_IPAD_Portrait[8][1] , 150, 150);
    Button9.frame = newFrame1;
    //__
    newFrame1 = CGRectMake(posArray_IPAD_Portrait[9][0] ,posArray_IPAD_Portrait[9][1] , 150, 150);
    Button10.frame = newFrame1;
    //__
    newFrame1 = CGRectMake(posArray_IPAD_Portrait[10][0] ,posArray_IPAD_Portrait[10][1] , 150, 150);
    Button11.frame = newFrame1;
    //__
    newFrame1 = CGRectMake(posArray_IPAD_Portrait[11][0] ,posArray_IPAD_Portrait[11][1] , 150, 150);
    Button12.frame = newFrame1;
    
    for(int i=0;i<[spriteArray count];i++){
        CGPoint point;
        point = CGPointMake(posArray_IPAD_Portrait[i][0], posArray_IPAD_Portrait[i][1]);
        [[spriteArray objectAtIndex:i] setPos:point];
    } 
}


#pragma mark -
#pragma mark Rotate to Landscape
- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
    // Remove the popover button.
    [navigationBar.topItem setLeftBarButtonItem:nil animated:NO];
    spielCounter.view.frame = CGRectMake(240, 0, 503, 44);
    spielCounter.myCounter.frame = spielCounter.view.frame;
    spielCounter.myCounter.textAlignment = UITextAlignmentCenter;
    
    imageResult.frame = CGRectMake((703-320)/2, 250, 387, 387);
    labelSupper.frame = CGRectMake((703-labelSupper.frame.size.width)/2, 293, labelSupper.frame.size.width, labelSupper.frame.size.height);
    
    richtigLabel.frame = CGRectMake((703-richtigLabel.frame.size.width)/2, 331, richtigLabel.frame.size.width, richtigLabel.frame.size.height);
    
    buttonNew.frame = CGRectMake((703-buttonNew.frame.size.width)/2, 383, buttonNew.frame.size.width, buttonNew.frame.size.height);
        
    CGRect newFrame1 = CGRectMake(posArray_IPAD_Lanscape[0][0] ,posArray_IPAD_Lanscape[0][1] , Button1.frame.size.width, Button1.frame.size.height);
    Button1.frame = newFrame1;
    NSLog(@"LANSCAPE:  %@",self.view);
    //__
    CGRect newFrame2 = CGRectMake(posArray_IPAD_Lanscape[1][0] ,posArray_IPAD_Lanscape[1][1] , Button2.frame.size.width, Button2.frame.size.height);
    Button2.frame = newFrame2;
    //__
    CGRect newFrame3 = CGRectMake(posArray_IPAD_Lanscape[2][0] ,posArray_IPAD_Lanscape[2][1] , 150, 150);
    Button3.frame = newFrame3;
    //__
    CGRect newFrame4 = CGRectMake(posArray_IPAD_Lanscape[3][0] ,posArray_IPAD_Lanscape[3][1] , 150, 150);
    Button4.frame = newFrame4;
    //__
    CGRect newFrame5 = CGRectMake(posArray_IPAD_Lanscape[4][0] ,posArray_IPAD_Lanscape[4][1] , 150, 150);
    Button5.frame = newFrame5;
    //__
    CGRect newFrame6 = CGRectMake(posArray_IPAD_Lanscape[5][0] ,posArray_IPAD_Lanscape[5][1] , 150, 150);
    Button6.frame = newFrame6;
    //__
    CGRect newFrame7 = CGRectMake(posArray_IPAD_Lanscape[6][0] ,posArray_IPAD_Lanscape[6][1] , 150, 150);
    Button7.frame = newFrame7;
    //__
    CGRect newFrame8 = CGRectMake(posArray_IPAD_Lanscape[7][0] ,posArray_IPAD_Lanscape[7][1] , 150, 150);
    Button8.frame = newFrame8;
    //__
    CGRect newFrame9 = CGRectMake(posArray_IPAD_Lanscape[8][0] ,posArray_IPAD_Lanscape[8][1] , 150, 150);
    Button9.frame = newFrame9;
    //__
    CGRect newFrame10 = CGRectMake(posArray_IPAD_Lanscape[9][0] ,posArray_IPAD_Lanscape[9][1] , 150, 150);
    Button10.frame = newFrame10;
    //__
    CGRect newFrame11 = CGRectMake(posArray_IPAD_Lanscape[10][0] ,posArray_IPAD_Lanscape[10][1] , 150, 150);
    Button11.frame = newFrame11;
    //__
    CGRect newFrame12 = CGRectMake(posArray_IPAD_Lanscape[11][0] ,posArray_IPAD_Lanscape[11][1] , 150, 150);
    Button12.frame = newFrame12;
    
    for(int i=0;i<[spriteArray count];i++){
        CGPoint point;
        point = CGPointMake(posArray_IPAD_Lanscape[i][0], posArray_IPAD_Lanscape[i][1]);
        [[spriteArray objectAtIndex:i] setPos:point];
    } 
    
}
//___END_NGUYEN_HUY_SON__________________________________________________________________________________________
//____NGUYEN_HUY_SON___
- (void)viewWillAppear:(BOOL)animated {
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    { 
        if([self isPadLandscape] == TRUE)
        {
            NSLog(@"%f",(703-spielCounter.view.frame.size.width)/2);

            imageResult.frame = CGRectMake((703-320)/2, 250, 320, 387);
            labelSupper.frame = CGRectMake((703-labelSupper.frame.size.width)/2, 293, labelSupper.frame.size.width, labelSupper.frame.size.height);
            
            richtigLabel.frame = CGRectMake((703-richtigLabel.frame.size.width)/2, 331, richtigLabel.frame.size.width, richtigLabel.frame.size.height);
            
            buttonNew.frame = CGRectMake((703-buttonNew.frame.size.width)/2, 383, buttonNew.frame.size.width, buttonNew.frame.size.height);
            
            
            CGRect newFrame1 = CGRectMake(posArray_IPAD_Lanscape[0][0] ,posArray_IPAD_Lanscape[0][1] , Button1.frame.size.width, Button1.frame.size.height);
            Button1.frame = newFrame1;
            //__
            CGRect newFrame2 = CGRectMake(posArray_IPAD_Lanscape[1][0] ,posArray_IPAD_Lanscape[1][1] , Button2.frame.size.width, Button2.frame.size.height);
            Button2.frame = newFrame2;
            //__
            CGRect newFrame3 = CGRectMake(posArray_IPAD_Lanscape[2][0] ,posArray_IPAD_Lanscape[2][1] , 150, 150);
            Button3.frame = newFrame3;
            //__
            CGRect newFrame4 = CGRectMake(posArray_IPAD_Lanscape[3][0] ,posArray_IPAD_Lanscape[3][1] , 150, 150);
            Button4.frame = newFrame4;
            //__
            CGRect newFrame5 = CGRectMake(posArray_IPAD_Lanscape[4][0] ,posArray_IPAD_Lanscape[4][1] , 150, 150);
            Button5.frame = newFrame5;
            //__
            CGRect newFrame6 = CGRectMake(posArray_IPAD_Lanscape[5][0] ,posArray_IPAD_Lanscape[5][1] , 150, 150);
            Button6.frame = newFrame6;
            //__
            CGRect newFrame7 = CGRectMake(posArray_IPAD_Lanscape[6][0] ,posArray_IPAD_Lanscape[6][1] , 150, 150);
            Button7.frame = newFrame7;
            //__
            CGRect newFrame8 = CGRectMake(posArray_IPAD_Lanscape[7][0] ,posArray_IPAD_Lanscape[7][1] , 150, 150);
            Button8.frame = newFrame8;
            //__
            CGRect newFrame9 = CGRectMake(posArray_IPAD_Lanscape[8][0] ,posArray_IPAD_Lanscape[8][1] , 150, 150);
            Button9.frame = newFrame9;
            //__
            CGRect newFrame10 = CGRectMake(posArray_IPAD_Lanscape[9][0] ,posArray_IPAD_Lanscape[9][1] , 150, 150);
            Button10.frame = newFrame10;
            //__
            CGRect newFrame11 = CGRectMake(posArray_IPAD_Lanscape[10][0] ,posArray_IPAD_Lanscape[10][1] , 150, 150);
            Button11.frame = newFrame11;
            //__
            CGRect newFrame12 = CGRectMake(posArray_IPAD_Lanscape[11][0] ,posArray_IPAD_Lanscape[11][1] , 150, 150);
            Button12.frame = newFrame12;
            for(int i=0;i<[spriteArray count];i++){
                CGPoint point;
                point = CGPointMake(posArray_IPAD_Lanscape[i][0], posArray_IPAD_Lanscape[i][1]);
                [[spriteArray objectAtIndex:i] setPos:point];
            } 
        }
        else
        {    
            imageResult.frame = CGRectMake((768-320)/2, 173, 320, 387);
            labelSupper.frame = CGRectMake((768-labelSupper.frame.size.width)/2, 293, labelSupper.frame.size.width, labelSupper.frame.size.height);

            richtigLabel.frame = CGRectMake((768-richtigLabel.frame.size.width)/2, 331, richtigLabel.frame.size.width, richtigLabel.frame.size.height);
            
            buttonNew.frame = CGRectMake((768-buttonNew.frame.size.width)/2, 383, buttonNew.frame.size.width, buttonNew.frame.size.height);
            
            NSLog(@"%f",(768-spielCounter.view.frame.size.width)/2);
            CGRect newFrame1 = CGRectMake(posArray_IPAD_Portrait[0][0] ,posArray_IPAD_Portrait[0][1] , 150, 150);
            Button1.frame = newFrame1;
            //__
            CGRect newFrame2 = CGRectMake(posArray_IPAD_Portrait[1][0] ,posArray_IPAD_Portrait[1][1] , 150, 150);
            Button2.frame = newFrame2;
            //__
            CGRect newFrame3 = CGRectMake(posArray_IPAD_Portrait[2][0] ,posArray_IPAD_Portrait[2][1] , 150, 150);
            Button3.frame = newFrame3;
            //__
            CGRect newFrame4 = CGRectMake(posArray_IPAD_Portrait[3][0] ,posArray_IPAD_Portrait[3][1] , 150, 150);
            Button4.frame = newFrame4;
            //__
            CGRect newFrame5 = CGRectMake(posArray_IPAD_Portrait[4][0] ,posArray_IPAD_Portrait[4][1] , 150, 150);
            Button5.frame = newFrame5;
            //__
            CGRect newFrame6 = CGRectMake(posArray_IPAD_Portrait[5][0] ,posArray_IPAD_Portrait[5][1] , 150, 150);
            Button6.frame = newFrame6;
            //__
            CGRect newFrame7 = CGRectMake(posArray_IPAD_Portrait[6][0] ,posArray_IPAD_Portrait[6][1] , 150, 150);
            Button7.frame = newFrame7;
            //__
            CGRect newFrame8 = CGRectMake(posArray_IPAD_Portrait[7][0] ,posArray_IPAD_Portrait[7][1] , 150, 150);
            Button8.frame = newFrame8;
            //__
            CGRect newFrame9 = CGRectMake(posArray_IPAD_Portrait[8][0] ,posArray_IPAD_Portrait[8][1] , 150, 150);
            Button9.frame = newFrame9;
            //__
            CGRect newFrame10 = CGRectMake(posArray_IPAD_Portrait[9][0] ,posArray_IPAD_Portrait[9][1] , 150, 150);
            Button10.frame = newFrame10;
            //__
            CGRect newFrame11 = CGRectMake(posArray_IPAD_Portrait[10][0] ,posArray_IPAD_Portrait[10][1] , 150, 150);
            Button11.frame = newFrame11;
            //__
            CGRect newFrame12 = CGRectMake(posArray_IPAD_Portrait[11][0] ,posArray_IPAD_Portrait[11][1] , 150, 150);
            Button12.frame = newFrame12;
            
            for(int i=0;i<[spriteArray count];i++){
                CGPoint point;
                point = CGPointMake(posArray_IPAD_Portrait[i][0], posArray_IPAD_Portrait[i][1]);
                [[spriteArray objectAtIndex:i] setPos:point];
            } 
        }
    }
    [super viewWillAppear:animated];
}

@end
